package cn.wgx.modules.sys.services;

import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysDuty;
import cn.wgx.modules.sys.entity.SysDutyExample;
import cn.wgx.modules.sys.entity.mapper.SysDutyMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysDutyService extends BaseService<SysDuty> {

    public static final String cacheName = "dutyCache";
    @Resource
    SysDutyMapper mapper;

    @Transactional(readOnly = true)
    @Cacheable(value = cacheName, unless="#result == null", key = "#root.target.getCachePreKey() + #record.toString() + #desc")
    public List<SysDuty> getList(SysDuty record, Boolean desc) {
		SysDutyExample example = new SysDutyExample();

		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysDuty> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysDutyExample example = new SysDutyExample();
            SysDutyExample.Criteria criteria = example.createCriteria();
            criteria.andDuty_idIn(uniList);
            List<SysDuty> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    @Cacheable(value = cacheName, unless="#result == null", key = "#root.target.getCachePreKey() + #id")
    public SysDuty getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public SysDuty insertOne( SysDuty record) {
        filter(record,true);
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public SysDuty updateOne( SysDuty record) {
        filter(record,false);
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    @CacheEvict(value = cacheName, allEntries = true)
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    protected void filter(SysDuty record, boolean isInsert){
        if(isInsert){

        }else{

        }
        super.filter(record, isInsert);
    }
	

}